package com.jdz.tools;

import com.jdz.dao.BothDao;
import com.jdz.dao.NoiseDao;
import com.jdz.entities.Both;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Merge {

    @Resource
    private BothDao bothDao;

    //首先，查询出 a<time<b 的数据
    //其次，
    public int merge(int step){//合并的步长
        int t = 0;//合并成功


        //为了保证时间复杂度，最好的方式是全部查出来，然后再n个一组

        ArrayList<Both> ab = bothDao.getBoth();

        List<Both> temp = new ArrayList<Both>();

        int flag = 0;
        for(Both b:ab)
        {
            flag+=step;
            if(b.getTime()<flag)
            {
                temp.add(b);
            }else
            {
                int len = temp.size();
                double[] avg_param = new double[8];
                Arrays.fill(avg_param,0);
                for(Both o:temp)
                {
                    avg_param[0]+=o.getTime();
                    avg_param[1]+=o.getTrack();


                    avg_param[2]+=o.getIndicated_airspeed();
                    avg_param[3]+=o.getTorque();
                    avg_param[4]+=o.getRotor_speed();
                    avg_param[5]+=o.getFuel_weight();
                    avg_param[6]+=o.getBarometric_height();
                    avg_param[7]+=o.getStatic_temperature();
                }
                for(int i=0;i<8;i++)
                {
                    avg_param[i] = avg_param[i]/len;
                }
                Both meg = new Both(avg_param);
                bothDao.addBoth();
                temp.clear();

            }
        }


        //时间从0开始。
        //怎样选择n个合并为一个

        return t;
    }

}
